package com.ajhlp.app.phoneDetailQuery.controller;


import javax.annotation.Resource;

import org.apache.ibatis.transaction.Transaction;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ajhlp.app.phoneDetailQuery.model.OperResult;
import com.ajhlp.app.phoneDetailQuery.service.IConfigModifyService;
import com.ajhlp.app.phoneDetailQuery.service.SystemConfigModifyService;

@Controller
public class ConfigModifyController {
	private Logger log = Logger.getLogger(getClass());
	
	@Resource
	private IConfigModifyService configModifyService;
	
	
	public IConfigModifyService getConfigModifyService() {
		return configModifyService;
	}
	public void setConfigModifyService(IConfigModifyService configModifyService) {
		this.configModifyService = configModifyService;
	}
	
	@RequestMapping(value="/config",method=RequestMethod.GET)
	public String toConfigPage(final ModelMap model){
		if(!SystemConfigModifyService.isAllConfigExist()){
			log.debug("load config from config.xml");
			configModifyService.readConfig();
		}
		
		model.put("configProps", IConfigModifyService.configProperties);
		
		OperResult operResult = new OperResult();
		operResult.setOper("list");
		operResult.setSuccess(true);
		operResult.setMsg("");
		
		model.put("operResult", operResult);
		
		return "bg/config";
	}
	
	@RequestMapping(value="/config",method=RequestMethod.POST)
	public String saveConfig(String query_url,String save_path,String query_num,String rule_min_v,final ModelMap model){
		OperResult operResult = new OperResult();
		operResult.setOper("save");
		try {
			configModifyService.saveConfig(query_url, save_path, query_num,rule_min_v);
			
			operResult.setSuccess(true);
			operResult.setMsg("");
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			log.error("保存系统参数配置失败", e);
			operResult.setSuccess(false);
			operResult.setMsg("");
		} 
		model.put("configProps", IConfigModifyService.configProperties);
		model.put("operResult", operResult);
		return "bg/config";
		
	}
}
